package org.smoozik.app.mode.linetrainer;

import java.awt.Component;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.smoozik.app.ihm.Constants;
import org.smoozik.app.mode.ModeSettings;
import org.smoozik.services.musictheory.Key;

public class LineTrainerSettings implements ModeSettings {
	JPanel mainPanel;
	LineTrainerMode mode;

	// private Smoozik parent;
	private JComboBox<Key> keyCombo;
	private JComboBox<Integer> numberCombo;
	private JComboBox<String> figureCombo;
	// private JComboBox<String> modeCombo;
	private JTextField speedTextField;
	private JTextField midTextField;
	private JCheckBox sharpBox;
	private JCheckBox renversementBox;
	private JCheckBox pianoBox;
	private JCheckBox partitionBox;
	private JRadioButton rtolsenseButton;
	private JRadioButton ltorsenseButton;

	private JRadioButton keybordButton;
	private JRadioButton buttonsButton;
	private JRadioButton midiButton;

	public LineTrainerSettings(LineTrainerMode mode) {
		this.mode = mode;
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		JPanel gameStyle = new JPanel();
		gameStyle.setLayout(new BoxLayout(gameStyle, BoxLayout.Y_AXIS));
		gameStyle.setBorder(new TitledBorder("Input / Output"));

		JPanel parameters = new JPanel();
		parameters.setLayout(new BoxLayout(parameters, BoxLayout.Y_AXIS));
		parameters.setBorder(new TitledBorder("Game Parameters"));

		mainPanel.add(gameStyle);
		mainPanel.add(parameters);

		keyCombo = new JComboBox<Key>();
		keyCombo.addItem(new Key(Constants.G_KEY));
		keyCombo.addItem(new Key(Constants.F_KEY));
		addComponent(parameters, Constants.KEY_LABEL, keyCombo);

		midTextField = new JTextField(5);
		addComponent(parameters, Constants.PARTMIDDLE_LABEL, midTextField);

		numberCombo = new JComboBox<Integer>();
		numberCombo.addItem(new Integer(3));
		numberCombo.addItem(new Integer(7));
		numberCombo.addItem(new Integer(15));
		numberCombo.addItem(new Integer(29));
		addComponent(parameters, Constants.NUMBER_LABEL, numberCombo);

		figureCombo = new JComboBox<String>();
		figureCombo.addItem(Constants.FIGURE_NOTE);
		figureCombo.addItem(Constants.FIGURE_INTERVAL);
		figureCombo.addItem(Constants.FIGURE_CHORD);
		addComponent(parameters, Constants.FIGURE_LABEL, figureCombo);

		sharpBox = new JCheckBox();
		addComponent(parameters, Constants.SHARPBEMOL_LABEL, sharpBox);

		renversementBox = new JCheckBox();
		addComponent(parameters, Constants.RENVERSEMENT_LABEL,
				renversementBox);

		JPanel sensePanel = new JPanel();
		speedTextField = new JTextField(5);
		addComponent(sensePanel, Constants.SPEED_LABEL, speedTextField);
		rtolsenseButton = new JRadioButton("Right to left");
		ltorsenseButton = new JRadioButton("Left to right");
		ButtonGroup group = new ButtonGroup();
		group.add(rtolsenseButton);
		group.add(ltorsenseButton);
		addComponent(sensePanel, null, rtolsenseButton);
		addComponent(sensePanel, null, ltorsenseButton);
		parameters.add(sensePanel);
		
		JPanel componentsPanel = new JPanel();
		pianoBox = new JCheckBox();
		addComponent(componentsPanel, Constants.PIANO_LABEL, pianoBox);
		partitionBox = new JCheckBox();
		addComponent(componentsPanel, Constants.PARTITION_LABEL, partitionBox);
		gameStyle.add(componentsPanel);

		JPanel inputPanel = new JPanel();
		keybordButton = new JRadioButton("Keyboard");
		buttonsButton = new JRadioButton("Buttons");
		midiButton = new JRadioButton("Midi Input");
		ButtonGroup inputGroup = new ButtonGroup();
		inputGroup.add(keybordButton);
		inputGroup.add(buttonsButton);
		inputGroup.add(midiButton);
		addComponent(inputPanel, null, keybordButton);
		addComponent(inputPanel, null, buttonsButton);
		addComponent(inputPanel, null, midiButton);
		gameStyle.add(inputPanel);

	}

	private void addComponent(JPanel origine, String text, Component component) {
		JPanel panel = new JPanel();
		if (text != null) {
			JLabel label = new JLabel(text);
			panel.add(label);
		}
		panel.add(component);
		origine.add(panel);
	}

	@Override
	public JPanel getMainPanel() {
		return this.mainPanel;
	}

	public void refreshParameters() {
		speedTextField.setText("" + this.mode.getTimerSpeed());
		keyCombo.setSelectedIndex(mode.getFigureManager().getKey());
		numberCombo.setSelectedItem(new Integer(this.mode.getFigureManager()
				.getNbNotes()));
		figureCombo.setSelectedItem(this.mode.getParamFigure());
		midTextField.setText("" + this.mode.getPartitionMidLevel());
		sharpBox.setSelected(mode.getParamSharpBemol() == 1);
		renversementBox.setSelected(mode.getParamRenversement() == 1);
		if (mode.getParamSense() == Constants.RIGHT_TO_LEFT) {
			rtolsenseButton.setSelected(true);
		} else {
			ltorsenseButton.setSelected(true);
		}
	}

	@Override
	public void saveParameters() {
		// Partition properties
		Key key = (Key) keyCombo.getSelectedItem();
		Integer number = (Integer) numberCombo.getSelectedItem();

		this.mode.updateKey(key.getKey());
		this.mode.updateNbNotes(number);
		this.mode.updateMidPart(midTextField.getText());

		// Generator properties
		if (sharpBox.isSelected()) {
			this.mode.updateSharpBemolParam(1);
		} else {
			this.mode.updateSharpBemolParam(0);
		}
		if (renversementBox.isSelected()) {
			this.mode.setParamRenversement(1);
		} else {
			this.mode.setParamRenversement(0);
		}
		String figure = (String) figureCombo.getSelectedItem();
		this.mode.updateParamFigure(figure);

		// UI
		if (speedTextField.getText() != null
				&& speedTextField.getText().trim().length() != 0) {
			this.mode.setTimerSpeed(Integer.parseInt(speedTextField.getText()));
		}
		if (ltorsenseButton.isSelected()) {
			this.mode.updateSenseParam(Constants.LEFT_TO_RIGHT);
		} else {
			this.mode.updateSenseParam(Constants.RIGHT_TO_LEFT);
		}

	}

}
